﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ZyGames.DZPocker.Tools
{
	class LinkedListNode<T>
	{
		private T value;
		private LinkedListNode<T> next;
		private LinkedListNode<T> prev;
		public LinkedListNode(T value)
		{
			this.value = value;
		}
		public T Value
		{
			get
			{
				return this.value;
			}
		}
		/// <summary>
		/// 后一个
		/// </summary>
		public LinkedListNode<T> Next
		{
			get { return next; }
			set { next = value; }
		}
		/// <summary>
		/// 前一个
		/// </summary>
		public LinkedListNode<T> Prev
		{
			get { return prev; }
			set { prev = value; }
		}
	}

	/// <summary>
	/// 环形的泛型链表
	/// </summary>
	/// <typeparam name="T"></typeparam>
	class LinkedList<T> : IEnumerable<T>
	{
		private int length;

		public int Length
		{
			get { return length; }
			set { length = value; }
		}

		private LinkedListNode<T> first;
		/// <summary>
		/// 第一个玩家
		/// </summary>
		internal LinkedListNode<T> First
		{
			get { return first; }
			set { first = value; }
		}

		private LinkedListNode<T> last;
		internal LinkedListNode<T> Last
		{
			get { return last; }
			set { last = value; }
		}
		/// <summary>
		/// 尾部追加
		/// </summary>
		/// <param name="node"></param>
		/// <returns></returns>
		public LinkedListNode<T> AddLast(T node)
		{
			LinkedListNode<T> newNode = new LinkedListNode<T>(node);
			if (first == null)
			{
				first = newNode;
				last = first;
			}
			else
			{
				last.Next = newNode;
				last = newNode;
			}
			return newNode;
		}

		/// <summary>
		/// 头部添加
		/// </summary>
		/// <param name="node"></param>
		/// <returns></returns>
		public LinkedListNode<T> AddFirst(T node)
		{
			LinkedListNode<T> newNode = new LinkedListNode<T>(node);
			if (first == null)
			{
				first = newNode;
				last = first;
			}
			else
			{
				newNode.Next = first;
				first = newNode;
			}
			return newNode;
		}

		/// <summary>
		/// 在某个节点之后插入
		/// </summary>
		/// <param name="node"></param>
		/// <returns></returns>
		public LinkedListNode<T> AddBehind(LinkedListNode<T> refNode, T node)
		{
			var newNode = new LinkedListNode<T>(node);
			if (first == null)
				return null;
			var temp = first;
			while (temp != null)
			{
				if (temp == refNode)
				{
					//最后一个节点
					if (temp.Next == null)
					{
						temp.Next = newNode;
						last = newNode;
					}
					else
					{
						newNode.Next = temp.Next;
						temp.Next = newNode;
					}
					return newNode;
				}
				else
				{
					temp = temp.Next;
				}
			}
			return null;
		}

		public LinkedListNode<T> GetNext(LinkedListNode<T> node)
		{
			if (IsLast(node))
				return first;
			else
				return node.Next;
		}

		public bool IsLast(LinkedListNode<T> node)
		{
			return node == Last;
		}

		public IEnumerator<T> GetEnumerator()
		{
			LinkedListNode<T> current = first;
			while (current != null)
			{
				yield return current.Value;
				current = current.Next;
			}
		}
		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}
	}

}